package com.doyens.user.controller;

import com.doyens.user.pojo.User;
import com.doyens.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUser(@PathVariable("data") String data,@PathVariable("type") Integer type){
        //调用service进行返回
        Boolean checkUser = userService.checkUser(data, type);
        if (checkUser == null){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(checkUser);

    }

    //发送短信验证码
    @PostMapping("/code")
    public ResponseEntity<Void> sendVerifyCode(@RequestParam("phone") String phone){
        this.userService.sendVerifyCode(phone);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("/register")
    public ResponseEntity<Void> register(@RequestParam("code") String code,@Valid User user){
        this.userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    //用户查询功能
    @PostMapping("query")
    public ResponseEntity<User> queryUser(@RequestParam("username") String username,@RequestParam("password")String password){
        User user = this.userService.queryUser(username,password);

        if (user == null){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(user);
    }
}
